package com.southwind.controller;


import com.southwind.entity.Menu;
import com.southwind.entity.MenuVo;
import com.southwind.entity.Type;
import com.southwind.feign.MenuFeign;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @program: aspringclouddemo
 * @description:
 * @author: xzf
 * @create: 2021-12-08 16:19
 **/
//@RestController  //这个注解是返回数据
@Controller //这个注解是返回视图
@RequestMapping("/menu")
public class MenuClientHandler {
    @Resource
    private MenuFeign menuFeign;
    @GetMapping("/findAll")
    @ResponseBody //这个注解告诉springboot我这个方法不返回视图 只是返回数据
    public MenuVo findAll(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit){
        Integer index=(page-1)*limit;
        return menuFeign.findAll(index, limit);
    }
//    返回前端视图
    @GetMapping("/redirect/{localhost}")
    public String view(@PathVariable("localhost") String localhost){
        return localhost;
    }

//    删除数据
    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") Integer id){
        menuFeign.deleteById(id);
        return "redirect:/menu/redirect/menu_manage";
    }
//    查询全部的菜品口味
    @GetMapping("/findTypes")
    public ModelAndView findTypes(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("menu_add");
        modelAndView.addObject("list",menuFeign.findTypes());
        return modelAndView;
    }

//    添加菜品
    @PostMapping("/save")
    public String save( Menu menu){

        menuFeign.save(menu);
        return "redirect:/menu/redirect/menu_manage";
    }
//    根据id查找数据
    @GetMapping("/findById/{id}")
    public ModelAndView findById(@PathVariable("id") Integer id){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("menu_update");
        modelAndView.addObject("menu",menuFeign.findById(id));
        modelAndView.addObject("list",menuFeign.findTypes());
        return modelAndView;
    }
//    修改菜品
    @PostMapping("/update")
    public String  update(Menu menu){
        menuFeign.update(menu);
        return "redirect:/menu/redirect/menu_manage";
    }
}
